package no45;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/5/22 11:04
 * 把数组排成最小的数
 * https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
 */
public class Solution {

    public String minNumber(int[] nums) {
        int n = nums.length, i = 0;
        String[] numsStr = new String[n];
        for (int num : nums){
            numsStr[i++] = String.valueOf(num); //将nums转为字符串
        }
        Arrays.sort(numsStr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                // 如果o1+o2 > o2+o1，说明o1大于o2,返回1，交换二者位置
                if ((o1+o2).compareTo(o2+o1) > 0)
                    return 1;
                return -1;
            }
        });
        StringBuilder str = new StringBuilder();
        for (String num : numsStr)
            str.append(num);
        return str.toString();
    }
}
